package cn.bkw.ytkapi.controller;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSON;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.bkw.ytkapi.services.UserInfoServices;
import cn.bkw.ytkapi.utils.ReturnJson;

import java.util.Map;

/**
 * 用户信息
 */
@Controller
public class UserInfoController {

    @Autowired
    UserInfoServices userInfoServices;

    //登录
    @RequestMapping(value = "/user/login", produces={"application/json;charset=utf-8"})
	@ResponseBody
	public String reciveScanInfo(HttpServletRequest request, HttpServletResponse rep, String username, String password){

    	String result = userInfoServices.login(request, username, password);
    	if(result != null && !result.equals("")){
			JSONObject json = JSONObject.fromObject(result);
			int status = json.getInt("status");
			if(status == 200 ) {
				JSONObject object = json.getJSONObject("obj");
				String uid = String.valueOf(object.getInt("id"));
				Cookie hc = new Cookie("admin_usercookie", uid); //用户id
				hc.setPath("/");
				hc.setMaxAge(60 * 60 * 24);
				rep.addCookie(hc);

				HttpSession session = request.getSession();
				session.setAttribute("uid", uid);
				session.setAttribute("username", object.getString("username"));
				session.setAttribute("realname", object.getString("realname"));
				session.setAttribute("roles", object.getJSONArray("roles"));//权限列表
				session.setAttribute("authorities", object.getJSONArray("authorities"));//权限列表

			}
		}

		return result;
	}

	/**
	 * 公共请求
	 * 拦截以crm2/开头的请求
	 * @param url 对应的API路径
	 * @param map map封装参数
	 * @param req
	 * @return String
	 * @throws Exception
	 */
	@RequestMapping(value = "/api/{url}", produces = {"application/json;charset=UTF-8"})
	@ResponseBody
	public String commonRequest1(
			@PathVariable String url, @RequestParam Map<String, String> map,
			HttpServletRequest req) throws Exception {
		if (url.indexOf("/") == -1) {
			url = "/" + url;
		}
		return userInfoServices.commonRequest(url, map, req);
	}

	/**
	 * 公共请求
	 * 拦截以api开头的请求
	 *
	 * @param path1 对应的API路径
	 * @param path2 对应的API路径
	 * @param map   map封装参数
	 * @param req
	 * @return String
	 * @throws Exception
	 */
	@RequestMapping(value = "/api/{path1}/{path2}", produces = {"application/json;charset=UTF-8"})
	@ResponseBody
	public String commonRequest2(
			@PathVariable String path1, @PathVariable String path2, @RequestParam Map<String, String> map,
			HttpServletRequest req) throws Exception {
		String url = "/" + path1 + "/" + path2;
		return userInfoServices.commonRequest(url, map, req);
	}

	/**
	 * 公共请求
	 * 拦截以api开头的请求
	 *
	 * @param path1 对应的API路径
	 * @param path2 对应的API路径
	 * @param map   map封装参数
	 * @param req
	 * @return String
	 * @throws Exception
	 */
	@RequestMapping(value = "/api/{path1}/{path2}/{path3}", produces = {"application/json;charset=UTF-8"})
	@ResponseBody
	public String commonRequest3(
			@PathVariable String path1, @PathVariable String path2, @PathVariable String path3, @RequestParam Map<String, String> map,
			HttpServletRequest req) throws Exception {
		String url = "/" + path1 + "/" + path2 + "/" + path3;
		return userInfoServices.commonRequest(url, map, req);
	}

}
